#ifndef _SF_FLASH_CONTROL_H_
#define _SF_FLASH_CONTROL_H_

#include <SwfUI.h>
#include <SFML/Graphics.hpp>

struct IViewObject;

namespace sf
{
	class FlashControl:
		public SwfUI::Control,
		public sf::Sprite,
		public sf::NonCopyable
	{
	public:
		FlashControl(int width, int height);

		virtual ~FlashControl();

		virtual void invalidateRect(int left, int top, int right, int bottom);		

		virtual void transformToLocal(int& x, int& y) const;

		static void updateAll();

	protected:
		virtual void Render(sf::RenderTarget& Target, sf::Renderer& Renderer) const;

	private:
		void resetDirty() const;

		void *dc,*bitmap,*old;
		mutable bool dirty;
		mutable int dtop,dleft,dright,dbottom;
		sf::Uint8* dib;
		sf::Uint8* subImage;
		IViewObject* view;	
		mutable sf::Image image;
	};
}

namespace SwfUI
{
	namespace
	{
		union ColorConverter
		{
			struct
			{
				sf::Uint8 r,g,b,a;
			} color;
			int value;
		};
	}

	//custom converter From SwfUI -> sf::Color
	template<>
	inline Argument::operator sf::Color () const
	{
		if(getType() != AT_NUMERIC)
			return sf::Color::Black;

		ColorConverter converter;
		converter.value = (int)(float)(*this);		
		return sf::Color(converter.color.r,converter.color.g,converter.color.b);
	}

	//convert from sf::Color to SwfUI
	template<>
	inline Argument::Argument(const sf::Color& value)
		:type(AT_NUMERIC)
	{
		ColorConverter converter;
		converter.color.r = value.r;
		converter.color.g = value.g;
		converter.color.b = value.b;
		converter.color.a = 0;
		this->value.numeric = (float)(converter.value);
	}
}

#endif